package mo.tags.stack;

import java.util.Arrays;
import java.util.Stack;

public class LV16_16 {

    public int[] subSort(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = Integer.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            while (!stack.isEmpty() && array[stack.peek()] > array[i]) {
                int cur = stack.pop();
                left = Math.min(left, cur);
            }
            stack.push(i);
        }
        stack.clear();
        int right = 0;
        for (int i = array.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && array[stack.peek()] < array[i]) {
                int cur = stack.pop();
                right = Math.max(right, cur);
            }
            stack.push(i);
        }
        return left == Integer.MAX_VALUE ? new int[]{-1, -1} : new int[]{left, right};
    }

    public static void main(String[] args) {
        LV16_16 test = new LV16_16();
        System.out.println(Arrays.toString(test.subSort(new int[]{1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19})));
    }

}
